/*
 * 
 */

package com.memorytech.starchive;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import jp.co.memory_tech.starchive.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import com.memorytech.starchive.dialog.BarcodeConfirmDialogFragment;
import com.memorytech.starchive.dialog.BarcodeSynDialogFragment;
import com.memorytech.starchive.httpservice.HttpClientHelper;
import com.memorytech.starchive.httpservice.AbstractMap.SimpleEntry;
import com.memorytech.starchive.httpservice.IService.Criteria;
import com.memorytech.starchive.httpservice.IService.Methods;
import com.memorytech.starchive.interfaceclass.OnAdvancedSearchResultListener;

/**
 * The Class AdvancedSearch.
 * 
 * @author Tam-LT
 */
public class AdvancedSearch extends Fragment implements OnClickListener, Observer,
        OnCheckedChangeListener {
    /** The view. */
    private View view;
    private Button search;
    private Spinner list_file;
    private HttpClientHelper httpClientHelper;
    private Criteria criteria;
    private RadioGroup radioGroup;
    private String tapeName;
    private String fileName;
    private OnAdvancedSearchResultListener mCallback;
    String[] file_type_array;

    /*
     * (non-Javadoc)
     * @see
     * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     * android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.advanced_search, container, false);
        search = (Button) view.findViewById(R.id.advanced_search);
        search.setOnClickListener(this);
        radioGroup = ((RadioGroup) view.findViewById(R.id.radio_group));
        if (radioGroup.getCheckedRadioButtonId() == R.id.and_radio) {
            criteria = Criteria.and;
        } else if (radioGroup.getCheckedRadioButtonId() == R.id.or_radio) {
            criteria = Criteria.or;
        }
        radioGroup.setOnCheckedChangeListener(this);

        list_file = (Spinner) view.findViewById(R.id.list_file);
        file_type_array = getResources().getStringArray(R.array.file_type_array);
        list_file.setAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_dropdown_item, file_type_array));
        // Inflate the layout for this fragment
        httpClientHelper = HttpClientHelper.getInstance();

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnAdvancedSearchResultListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    /*
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.advanced_search:
                tapeName = ((EditText) view.findViewById(R.id.advanced_search_tape)).getText()
                        .toString();
                fileName = ((EditText) view.findViewById(R.id.advanced_search_file)).getText()
                        .toString();
                mCallback.onSearchResult(null);

                // httpClientHelper.addObserver(this);
                // httpClientHelper.advancedSearch(tapeName, fileName, "",
                // Criteria.and);
                // BarcodeSynDialogFragment.show(getActivity(),
                // "Loading data from server");
                break;

            default:
                break;
        }

    }

    /*
     * (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    @Override
    public void update(Observable observable, Object data) {
        // TODO Auto-generated method stub
        if (observable instanceof HttpClientHelper) {

            @SuppressWarnings("unchecked")
            SimpleEntry<Methods, List<Object>> ret = (SimpleEntry<Methods, List<Object>>) data;
            // BarcodeSynDialogFragment.dismiss(getActivity());
            switch (ret.getKey()) {
                case advancedSearch:
                    // httpClientHelper.deleteObserver(this);
                    mCallback.onSearchResult(null);
                    break;

                default:
                    break;
            }

        }
    }

    /*
     * (non-Javadoc)
     * @see
     * android.widget.RadioGroup.OnCheckedChangeListener#onCheckedChanged(android
     * .widget.RadioGroup, int)
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        switch (checkedId) {
            case R.id.and_radio:
                criteria = Criteria.and;
                break;
            case R.id.or_radio:
                criteria = Criteria.or;
                break;

            default:
                break;

        }
    }

}
